package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.GoodsEntity;
import cn.mumzone.pos.ui.widgets.shopping.AnimShopButton;

/**
 * Created by JoeLjt on 2019/6/28.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class ADA_DialogChooseGoods extends CommonAdapter<GoodsEntity> {

    private boolean isBindMember;

    public ADA_DialogChooseGoods(Context context, boolean isBindMember) {
        super(context);
        this.isBindMember = isBindMember;
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_choose_goods_list;
    }

    @Override
    protected void convert(ViewHolder holder, final GoodsEntity goodsEntity, int position) {
        holder.setText(R.id.tv_good_name, goodsEntity.getGoodsName());

        holder.setText(R.id.tv_good_spec, getGoodSpecString(goodsEntity.getGoodsSpec()));

        TextView tvOriginPrice = holder.getView(R.id.tv_good_origin_price);
        TextView tvPrice = holder.getView(R.id.tv_good_price);

        // 原价，默认划掉
        tvOriginPrice.setVisibility(View.VISIBLE);
        tvOriginPrice.setText("￥" + goodsEntity.getPrice());
        tvOriginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

        if (StringUtils.isNumber(goodsEntity.getBargainPrice()) && Double.parseDouble(goodsEntity.getBargainPrice()) != 0) {

            // 如果有议价，展示议价
            tvPrice.setText("￥" + goodsEntity.getBargainPrice());

        } else {

            // 如果有会员价，展示会员价
            if (isBindMember && Double.parseDouble(BigDecimalUtil.getNumber(goodsEntity.getMemberPrice())) != 0) {
                tvPrice.setText("￥" + goodsEntity.getMemberPrice());
            } else {
                tvOriginPrice.setVisibility(View.GONE);
                tvPrice.setText("￥" + goodsEntity.getPrice());
            }
        }

        // 加减控件
        AnimShopButton vAmount = holder.getView(R.id.v_goods_amount);
        if (Integer.parseInt(goodsEntity.getGoodsBatchNum()) == 1) {
            vAmount.setMaxCount(Integer.parseInt(goodsEntity.getGoodsQuantity()));
        }
        vAmount.setOnAddDelListener(new GoodsShopButtonAdapter(mContext) {
            @Override
            protected void onAmountChanged(int count) {
                goodsEntity.setGoodsCount(StringUtils.get(count));
                goodsEntity.setSelectCount(count);
                if (mListener != null) {
                    mListener.onAmountChanged();
                }
            }

            @Override
            public boolean beforeAddClick() {
                if (StringUtils.isTrue(goodsEntity.getStockGoods())) {
                    PosToast.INSTANCE.makeShortToast(mContext, "商品未入库，无法售卖~");
                    return true;
                }
                return false;
            }

            @Override
            public boolean beforeDelClick() {
                if (StringUtils.isTrue(goodsEntity.getStockGoods())) {
                    PosToast.INSTANCE.makeShortToast(mContext, "商品未入库，无法售卖~");
                    return true;
                }
                return false;
            }
        });

    }

    private String getGoodSpecString(List<GoodsEntity.GoodsSpecBean> goodsSpec) {
        StringBuilder builder = new StringBuilder();
        builder.append("规格：");
        for (int i = 0; i < goodsSpec.size(); i++) {
            GoodsEntity.GoodsSpecBean goodsSpecBean = goodsSpec.get(i);
            builder.append(goodsSpecBean.getValue());
            if (i < goodsSpec.size() - 1) {
                builder.append(",");
            }
        }
        return builder.toString();
    }

    public ArrayList<GoodsEntity> getSelectedList() {
        ArrayList<GoodsEntity> goodsList = new ArrayList<>();
        if (!ListUtil.isListEmpty(getDataList())) {
            for (GoodsEntity entity : getDataList()) {
                if (StringUtils.isNumber(entity.getGoodsCount())) {
                    if (Integer.parseInt(entity.getGoodsCount()) > 0) {
                        goodsList.add(entity);
                    }
                }
            }
        }
        return goodsList;
    }

    public int getSelectedCount() {
        int totalCount = 0;
        ArrayList<GoodsEntity> selectedList = getSelectedList();
        for (GoodsEntity goodsEntity : selectedList) {
            totalCount += Integer.parseInt(goodsEntity.getGoodsCount());
        }
        return totalCount;
    }

    public interface OnGoodsAmountChangedListener {
        void onAmountChanged();
    }

    private OnGoodsAmountChangedListener mListener;

    public void setOnGoodsAmountChangedListener(OnGoodsAmountChangedListener mListener) {
        this.mListener = mListener;
    }
}
